package algorithms.search

/**
 * [Pairs](https://www.hackerrank.com/challenges/pairs)
 */
// Complete the pairs function below.
fun pairs(k: Int, arr: Array<Int>): Int {
    var result = 0
    val sorted = arr.sorted()
    var it: Int
    for (i in 0 until sorted.size - 1) {
        it = sorted[i]
        if (sorted.binarySearch(it + k, i, sorted.size) >= 0) {
            result++
        }
    }
    return result
}